﻿using Hopscotch.Common.Contracts.AppService;
using Hopscotch.Presentation.Common.Commands;
using Hopscotch.Presentation.Common.Threading;
using Hopscotch.Test.Presentation.Threading;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using Hopscotch.Test.Presentation;

namespace Hopscotch.Presentation.Common.Test.Commands
{
    [TestFixture]
    public class SaveEntityCommandTest
    {
        private SaveEntityCommand<TestEntity, int> saveEntityCommandTest;

        private Mock<IToolViewModel<TestEntity, int>> mockTestEntityViewModel;
        private Mock<IDomainService<TestEntity, int>> mockTestEntityService;


        [SetUp]
        public void SetUp()
        {
            IDispatcher dispatcher = new SynchronousDispatcher();
            mockTestEntityViewModel = new Mock<IToolViewModel<TestEntity, int>>();
            mockTestEntityService = new Mock<IDomainService<TestEntity, int>>();

            saveEntityCommandTest = new SaveEntityCommand<TestEntity, int>(dispatcher, mockTestEntityViewModel.Object, mockTestEntityService.Object);
        }

        [Test]
        public void TestExecuteShouldSetIsSavePressed()
        {
            mockTestEntityViewModel.SetupProperty<bool>(p => p.IsSavePressed);
            saveEntityCommandTest.Execute(null);
            mockTestEntityViewModel.Object.IsSavePressed.Should().BeTrue();
        }

        [Test]
        public void TestExecuteShouldSetToolEditModeToNone()
        {
            mockTestEntityViewModel.SetupProperty<ToolEditMode>(p => p.ToolEditMode);

            var toolViewModel = mockTestEntityViewModel.Object;

            toolViewModel.ToolEditMode = ToolEditMode.New;

            //Use AsyncPump as nunit does not support async void tests
            AsyncPump.Run(() => saveEntityCommandTest.Execute(null));

            toolViewModel.ToolEditMode.Should().Be(ToolEditMode.None);
        }

        [Test]
        public void TestExecuteShouldCallValidate()
        {
            saveEntityCommandTest.Execute(null);
            mockTestEntityViewModel.Verify(s => s.Validate(), Times.Once);
        }


    }
}
